// event loop
/**Q1*/
test1(){
  Promise.resolve().then(()=>{
    console.log('promise1');
    setTimeout(()=>{
      console.log('setTimeout2');
    },0)
  });
  setTimeout(()=>{
    console.log('setTimeout1');
    Promise.resolve().then(()=>{
      console.log('promise2')
    })
  },0)
}


/**Q2*/
test2(){
  console.log('script start');
  setTimeout(function(){
    console.log('setTimeout');
  },0);
  Promise.resolve().then(function(){
    console.log('promise1');
  }).then(function(){
    console.log('promise2')
  });
  console.log('script end')
}


/**Q3 async await更多例子可参考：https://blog.csdn.net/roamingcode/article/details/85052590*/
test3(){
  async function async1(){
    console.log('async1 start');
    await async2();
    console.log('async1 end');
  }
  async function async2(){
    console.log('async2');
  }
  console.log('script start');
  setTimeout(function(){
    console.log('setTimeout')
  })
  async1()
  new Promise(function(resolve){
    console.log('promise1')
    resolve()
  }).then(function(){
    console.log('promise2')
  })
  console.log('script end');
}

/**Q4 */
test4(){
  console.log(1);
  setTimeout(()=>{
    console.log(2)
  },10);
  new Promise(function(resolve){
    console.log(3);
    resolve();
    console.log(4);
  }).then(function(){
    console.log(5)
  });
  console.log(6);
  requestIdleCallback(()=>console.log(7));
  requestAnimationFrame(()=>console.log(8));
}


/**Q5 */
setTimeout(()=>{
    console.log(1)
},20);
console.log(2);
setTimeout(()=>{
    console.log(3);
},10);
console.log(4);
console.time('AA');
for(let i=0;i<900000000;i++){

}
console.timeEnd('AA');
console.log(5);
setTimeout(()=>{
    console.log(6)
},8);
console.log(7);
setTimeout(()=>{
    console.log(8);
},15);
console.log(9);







// answers:


/**Q1 answer */
//promise1
//setTimeout1
//promise2
//setTimeout2

/**Q2 answer */
//script start
// script end
// promise1
//promise2
// setTimeout

/**Q3 answer */
// script start
// async1 start
// async2
// promise1
// script end
// async1 end
// promise2
// setTimeout

/**Q4 answer */
//1
//3
//4
//6
//5
//2
//8 7



